<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商品ID">
          <el-input v-model="form.goods_id" placeholder="商品ID" />
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.name" placeholder="商品名称" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="goods_id" label="商品ID" align="center" width="150" />
          <el-table-column prop="channel" label="APP渠道" align="center" width="100" />
          <el-table-column prop="name" label="商品名称" align="center" width="150" />
          <el-table-column label="商品图片" align="center" width="100">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.img_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.img_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="number" label="钻石数量" align="center" width="100" />
          <el-table-column prop="price" label="价格" align="center" />
          <el-table-column prop="discount_price" label="折扣" align="center" />
          <el-table-column prop="price" label="价格" align="center" />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="symbol" label="币种符号" align="center" />
          <el-table-column prop="show_price" label="展示价格" align="center" />
          <el-table-column prop="show_currency" label="展示币种" align="center" />
          <el-table-column prop="show_symbol" label="展示符号" align="center" />
          <el-table-column prop="description" label="描述" align="center" width="150" />
          <el-table-column prop="rank" label="排序" align="center" />
          <el-table-column prop="createtime" label="添加时间" align="center" width="180" :formatter="formatterTime"/>
          <el-table-column label="操作" align="center" fixed="right" width="120">
            <template #default="scope">
              <el-button type="primary" plain @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";

import { formatterTime } from "@/utils/utils";
import { goodsListApi, operateGoodsApi } from "@/api/moby/shop/shop";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const btnType = ref(false);
const formData = {
  goods_id: "",
  name: ""
};
const form = reactive({ ...formData });
const dialogFormData = {
  goods_id: "",
  name: "",
  img: "",
  number: "",
  price: "",
  discount_price: "",
  currency: "",
  symbol: "",
  show_price: "",
  show_currency: "",
  show_symbol: "",
  description: "",
  rank: "",
  id:""
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    goods_id: form.goods_id,
    name: form.name
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await goodsListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 新增
const onAdd = () => {
  dialogVisible.value = true;
  btnType.value = 'add'
};

// 编辑
const handleEdit = async (index, row) => {
  dialogVisible.value = true;
  Object.assign(dialogForm, { ...row });
  btnType.value = 'edit'
};

//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async param => {
  try {
    btnLoading.value = true;
    if(btnType == 'add') {
      delete param.id
    }
    const res = await operateGoodsApi(param);
    ElMessage({
      message: res.message,
      type: "success"
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
const handleUploader = (res, name, src) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success"
    });

    dialogForm[src] = res.data.pic_url || "";
    dialogForm[name] = res.data.name || "";
  }
};

// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
